package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //只出现一次的数字3
    public int[] singleNumber3(int[] nums){
        int eor = 0;
        int[] res = new int[2];
        for (int num : nums) {
            eor ^= num;//得到的结果是a^b
        }
        int rightOne =eor&(~eor+1);
        for (int num : nums) {
            if ((rightOne & num) == 0) {
                res[0]^=num;
            }
        }
        res[1] = res[0] ^ eor;
        return res;
    }


    //只出现一次的数字1
    public int singleNumber(int[] nums) {
        int ret = nums[0];
        for (int i = 1; i < nums.length; i++) {
            ret ^= nums[i];
        }
        return ret;
    }

    //汉明距离
    public int hammingDistance(int x, int y) {
        int n = x^y;
        int ret = 0;
        while (n > 0) {
            if((n&1)==1) ret++;
            n>>=1;
        }
        return ret;
    }


    public int[] countBits(int n) {
        int[] bits = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            bits[i] = countOnes(i);
        }
        return bits;
    }

    public int countOnes(int x) {
        int ones = 0;
        while (x > 0) {
            x &= (x - 1);
            ones++;
        }
        return ones;
    }


    //比特位计数
    public int[] countBits1(int n) {
        int[] ret = new int[n+1];
        for (int i = 0; i <= n; i++) {
            int count = 0;
            int a = n;
            for (int j = 0; j < 32; i++) {
                if((a&1)==1)
                    count++;
                a>>=1;
            }
            ret[i] = count;
            n--;
        }
        return ret;
    }

    //位1的个数
    public int hammingWeight(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if((n&1)==1)
                count++;
            n>>=1;
        }
        return count;
    }

    //平方数之和
    public boolean judgeSquareSum(int c) {
        for (int i = 0; i * i<=c ; i++) {
            int b = (int)Math.sqrt(c-i*i);
            if((i*i+b*b==c)) return true;
        }
        return false;
    }
}
